<?php
/**
 * OrderDetail.php
 * Author: luoji (867404364@qq.com)
 * Date: 2020-12-20
 * Time: 12:49
 */

namespace App\Tables;


class OrderDetail extends Base
{

    const Outsource_Paid_Ok = 1;
    const Outsource_Paid_No = 0;

    public static function getTotalPrice($countType, $price, $width, $height, $number)
    {
        $w = $width > 0 ? $width / 1000 : 0;
        $h = $height > 0 ? $height / 1000 : 0;
        switch ((int) $countType) {
            case Business::COUNT_TYPE_1:
                return $w*$h*$price*$number;
                break;
            case Business::COUNT_TYPE_2:
                $maxLength = $w > $h ? $w : $h;
                return $maxLength * $price * $number;
                break;
            case Business::COUNT_TYPE_3:
                return $number * $price;
                break;
            default:
                return 0;
        }
    }

    public static function getArea($countType, $width, $height)
    {
        $w = $width > 0 ? $width / 1000 : 0;
        $h = $height > 0 ? $height / 1000 : 0;

        switch ((int) $countType) {
            case Business::COUNT_TYPE_1:
                return sprintf('%.2f', $w*$h);
                break;
            case Business::COUNT_TYPE_2:
                $maxLength = $w > $h ? $w : $h;
                return sprintf('%.2f', $maxLength);
                break;
            case Business::COUNT_TYPE_3:
                return 0;
                break;
            default:
                return 0;
        }
    }

    public static function getRightImg($url)
    {
        if(empty($url)) { return $url; }
        $parse = parse_url($url);
        if(empty($parse['path'])) { return $url; }
        return "http://img.tiaofu.vip".$parse['path'];
    }

    public function consumables()
    {
        return $this->hasMany(OrderConsumable::class, 'od_id', 'id');
    }

    public function postPrints()
    {
        return $this->hasMany(OrderPostPrint::class, 'od_id', 'id');
    }

    public function order()
    {
        return $this->belongsTo(Order::class, 'oid', 'id');

    }
}